package com.sailors.common.task.sdk.config;

import com.alibaba.fastjson.JSONObject;
import com.sailors.common.task.sdk.config.property.PropertyLoader;
import com.sailors.common.task.sdk.core.constants.ApiConstants;
import com.sailors.common.task.sdk.core.handle.KafkaProducer;
import com.sailors.common.task.sdk.core.handle.worker.TaskWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Sdk initk runner
 *
 * @description: 初始化
 * @author: cyj
 * @date 2022 -11-25 14:46:19
 */
@Component
@Slf4j
public class SdkInitkRunner implements InitializingBean {

    /**
     * Active
     */
    @Value("${spring.profiles.active}")
    private String active;

    /**
     * Property loader
     */
    @Autowired
    private PropertyLoader propertyLoader;

    @Override
    public void afterPropertiesSet() {
        // 读取配置文件
        JSONObject kafkaConfig = propertyLoader.loadKafkaConfig(active);
        String urlConfig = propertyLoader.loadUrlConfig(active);
        if (null == kafkaConfig || StringUtils.isEmpty(urlConfig)) {
            log.warn("缺少sdk配置, common task sdk 启动失败！");
            return;
        }
        // 初始化kafka生产者
        KafkaProducer.initConfig(kafkaConfig);
        // 初始化api基本地址
        ApiConstants.PREFIX = urlConfig;
        // 任务工作线程开启
        TaskWorker.start();
    }
}
